/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

/**
 *
 * @author VAN ANH
 */
import java.sql.*;
import java.util.Hashtable;

public class Common {
  
  static final int UNDEFINT = Integer.MIN_VALUE;
  static final String DBDriver  ="com.mysql.jdbc.Driver";
  static final String strConn   ="jdbc:mysql://localhost:3306/banhang"; 
  
  static final String DBusername="root";
  static final String DBpassword="";
  
  public Common(){}
  
  public static String loadDriver () {
    String sErr = "";
    try {
       Class.forName(DBDriver);
    }
    catch (Exception e) {
      sErr = e.toString();
    }
    return (sErr);
  }
  
  public Connection cn() throws SQLException {
    return DriverManager.getConnection(strConn , DBusername, DBpassword);
  }
  
  public ResultSet openrs(Statement stat, String sql) throws SQLException {
    ResultSet rs = stat.executeQuery(sql);
    return (rs);
  }
  
  public static void absolute(ResultSet rs, int row) throws SQLException{
    for(int x=1;x<row;x++) {
          rs.next();
      }
  }

  boolean isEmpty (String val){
    return (val==null || val.equals("")||val.equals(Integer.toString(UNDEFINT))); 
  }
  
  String getValue(ResultSet rs, String strFieldName) {
    if ((rs==null) ||(isEmpty(strFieldName)) || ("".equals(strFieldName))) {
          return "";
      }
    try {
      String sValue = rs.getString(strFieldName);
      if ( sValue == null ) sValue = "";
      return sValue;
    }
    catch (Exception e) {
      return "";
    }
  }
  
  public Hashtable getRecordToHash (ResultSet rs, Hashtable rsHash, String[] aFields ) throws SQLException {
    for ( int iF = 0; iF < aFields.length; iF++ ) {
      rsHash.put( aFields[iF], getValue(rs, aFields[iF]));
    }
    return rsHash;
  }
  
  public String[] getFieldsName (ResultSet rs ) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    int count = metaData.getColumnCount();
    String[] aFields = new String[count];
    for(int j = 0; j < count; j++) {
      aFields[j] = metaData.getColumnLabel(j+1);
    }
    return aFields;
  }
  private String replace(String str, String pattern, String replace) {
    if (replace == null) {
      replace = "";
    }
    int s = 0, e = 0;
    StringBuffer result = new StringBuffer((int) str.length()*2);
    while ((e = str.indexOf(pattern, s)) >= 0) {
      result.append(str.substring(s, e));
      result.append(replace);
      s = e + pattern.length();
    }
    result.append(str.substring(s));
    return result.toString();
  }
   public String getParam(javax.servlet.http.HttpServletRequest req, String paramName) {
    String param = req.getParameter(paramName);
    if ( param == null || param.equals("") ) {
           return "";
       }
    param = replace(param,"&amp;","&");
    param = replace(param,"&lt;","<");
    param = replace(param,"&gt;",">");
    param = replace(param,"&amp;lt;","<");
    param = replace(param,"&amp;gt;",">");
    return param;
  }
  
}
